package yt.kpool.activity.setting;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.home.CalculatorAc;
import yt.kpool.bean.GetWarnsBean;
import yt.kpool.bean.SetPoliceBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pickerview.TimePickerPopWin;

/**
 * 报警设置
 */
public class PoliceSet extends AppCompatActivity {
    @BindView(R.id.img_1)
    ImageButton img_1;
    @BindView(R.id.img_2)
    ImageButton img_2;
    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.ly_1)
    LinearLayout ly_1;
    @BindView(R.id.ly_2)
    LinearLayout ly_2;
    private boolean b,b1;
    private LoadingDialog dialog;
    private String StartHour,StartMinute, EndHour, EndMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policeset);
        ButterKnife.bind(this);
        initTitle();
        init();
        if(NetWorkUtil.checkNet(PoliceSet.this))
        {
            getNetData();
        }
        else
        {

            ToastUtil.showToast(PoliceSet.this,getResources().getString(R.string.internet_error));
        }

    }
    private void init()
    {
        dialog = new LoadingDialog(this, 0);
    }

    private void getNetData()
    {   dialog.show();
        NetHelperNew.GetOfflineWarnsModel(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                GetWarnsBean getWarnsBean = MyGson.getInstance().fromJson(data, GetWarnsBean.class);
                if(getWarnsBean.getType()==1)
                {
                    if(getWarnsBean.getData().isEnable())
                    {
                        img_1.setBackgroundResource(R.mipmap.kai);
                        ly_1.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        img_1.setBackgroundResource(R.mipmap.guan);
                        ly_1.setVisibility(View.GONE);
                    }
                    if(getWarnsBean.getData().isNoDisturbing())
                    {
                        img_2.setBackgroundResource(R.mipmap.kai);
                        ly_2.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        img_2.setBackgroundResource(R.mipmap.guan);
                        ly_2.setVisibility(View.GONE);
                    }
                    b=getWarnsBean.getData().isEnable();
                    b1=getWarnsBean.getData().isNoDisturbing();
                    StartHour=getWarnsBean.getData().getStartHour()+"";
                    StartMinute=getWarnsBean.getData().getStartMinute()+"";
                    EndHour=getWarnsBean.getData().getEndHour()+"";
                    EndMinute=getWarnsBean.getData().getEndMinute()+"";

                    tv_1.setText(getNumber(getWarnsBean.getData().getStartHour())+":"+getNumber(getWarnsBean.getData().getStartMinute()));
                    tv_2.setText(getNumber(getWarnsBean.getData().getEndHour())+":"+getNumber(getWarnsBean.getData().getEndMinute()));

                }
                else
                {
                    Toast.makeText(PoliceSet.this,getWarnsBean.getContent(),Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                Toast.makeText(PoliceSet.this,msg,Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

    }
    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("报警设置");

    }

    private String getNumber(int m)
    {
        String mm="";
        if(m<10)
        {
            mm="0"+m;
        }
        else
        {
            mm=m+"";
        }
        return mm;
    }

    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.img_1, R.id.img_2,R.id.tv_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_1:
                if(!b)
                {
                    img_1.setBackgroundResource(R.mipmap.kai);
                    b=true;
                    ly_1.setVisibility(View.VISIBLE);
                }
                else
                {
                    img_1.setBackgroundResource(R.mipmap.guan);
                    b=false;
                    ly_1.setVisibility(View.GONE);
                }
                setNetData(b+"",b1+"",StartHour,StartMinute,EndHour,EndMinute);
                break;
            case R.id.img_2:
                if(!b1)
                {
                    img_2.setBackgroundResource(R.mipmap.kai);
                    b1=true;
                    ly_2.setVisibility(View.VISIBLE);
                }
                else
                {
                    img_2.setBackgroundResource(R.mipmap.guan);
                    b1=false;
                    ly_2.setVisibility(View.GONE);
                }
                setNetData(b+"",b1+"",StartHour,StartMinute,EndHour,EndMinute);
                break;
            case R.id.tv_update:
                TimePickerPopWin timePickerPopWin=new TimePickerPopWin.Builder(PoliceSet.this, new TimePickerPopWin.OnTimePickListener() {
                    @Override
                    public void onTimePickCompleted(int hour, int minute, String AM_PM, String time,String h1,String m1,String h2,String m2) {
                        tv_1.setText(h1+":"+m1);
                        tv_2.setText(h2+":"+m2);
                        StartHour=h1;
                        StartMinute=m1;
                        EndHour=h2;
                        EndMinute=m2;
                        setNetData(b+"",b1+"",StartHour,StartMinute,EndHour,EndMinute);

                    }
                },StartHour,StartMinute,EndHour,EndMinute).textConfirm("确定")
                        .textCancel("取消")
                        .btnTextSize(16)
                        .viewTextSize(25)
                        .colorCancel(Color.parseColor("#a9a9a9"))
                        .colorConfirm(Color.parseColor("#0c7fe9"))
                        .build();
                timePickerPopWin.showPopWin(PoliceSet.this);
                break;
        }
    }

    /**
     * 设置报警
     * @param Enable
     * @param NoDisturbing
     * @param StartHour
     * @param StartMinute
     * @param EndHour
     * @param EndMinute
     */
    private void setNetData(String Enable,String NoDisturbing,String StartHour,String StartMinute,String EndHour,String EndMinute)
    {

        if(NetWorkUtil.checkNet(PoliceSet.this))
        {
            dialog.show();
            NetHelperNew.SetOfflineWarns(Enable, NoDisturbing, StartHour, StartMinute, EndHour, EndMinute, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    SetPoliceBean setPoliceBean = MyGson.getInstance().fromJson(data, SetPoliceBean.class);
                    if(setPoliceBean.getType()==1)
                    {

                        Toast.makeText(PoliceSet.this,setPoliceBean.getContent(),Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(PoliceSet.this,setPoliceBean.getContent(),Toast.LENGTH_SHORT).show();
                    }
                    dialog.dismiss();
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    dialog.dismiss();
                    Toast.makeText(PoliceSet.this,msg,Toast.LENGTH_SHORT).show();
                }
            });
        }
        else
        {

            ToastUtil.showToast(PoliceSet.this,getResources().getString(R.string.internet_error));
        }

    }
}
